﻿using System;
using System.Drawing;
using System.Drawing.Printing;
using DevExpress.XtraReports.UserDesigner;
using RedRiver.Framework.XtraCommon.FormHelper;
using RedRiver.Framework.XtraCommon.Properties;
using RedRiver.Framework.XtraCommon.Reports.Entities;
using RedRiver.Framework.XtraCommon.Templates;
using RedRiver.Framework.XtraCommon.Templates.Generic;
using RedRiver.Framework.XtraCommon.UserAuthentication;

namespace RedRiver.Framework.XtraCommon.Reports{
    public partial class ReportLayoutList : EntityDataGridViewForm{
        public ReportLayoutList(){
            InitializeComponent();
            InitDataContext<ReportEntities, ReportLayout>();
            ManipulateRule = DataManipulateRule.All;
            defaultBindingSource.AddingNew += (s, e) =>{
                                                  e.NewObject = new ReportLayout{
                                                                                      ReportId = Guid.NewGuid(),
                                                                                      PaperName = string.Empty,
                                                                                      PaperKind = (int) PaperKind.Custom,
                                                                                      AppMomain = UserContext.GlobalConst.ApplicationName
                                                                                  };
                                              };
        }

        public override Image TitleImage {
            get {
                return Resources.Action_Report_ShowDesigner_32x32;
            }
        }

        public new EntityDataContext<ReportEntities> DataContext {
            get {
                return (EntityDataContext<ReportEntities>)base.DataContext;
            }
            set {
                base.DataContext = value;
            }
        }

        private ReportLayout Current{
            get { return (ReportLayout) defaultBindingSource.Current; }
        }

        protected override void InitBarItems(){
            base.InitBarItems();
            var toolBar = BarManager.ToolBar;

            toolBar.AddItem(toolBar.BarItemShowShowReportDesigner);

            toolBar.ShowReportDesigner = EditReport;
            toolBar.DataEdit = EditReport;
        }

        protected override void InitEditors() {
            base.InitEditors();
            new EnumValueSelector(PaperKind.Custom).AttachEdit(colPaperKind);
        }

        private void EditReport(){
            XtraMessageForm.ShowMessage("正在初始化报表编辑器...");
            var report = Current.GetReport();
            using (var designer = new XRDesignFormEx()) {
                designer.OpenReport(report);
                designer.Closed += (s, e) => {
                    if (designer.DesignPanel.ReportState == ReportState.Saved) {
                        Current.SerializaReport(designer.DesignPanel.Report);
                        UpdateData();
                    }
                };
                XtraMessageForm.Hiden();
                designer.ShowDialog(this);
            }
        }
    }
}